package com.liuguang.testassert.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.liuguang.testassert.common.Result;
import com.liuguang.testassert.dao.UserMapper;
import com.liuguang.testassert.enums.CheckParamEnums;
import com.liuguang.testassert.enums.ResultFlag;
import com.liuguang.testassert.pojo.User;
import com.liuguang.testassert.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

/**
 * @ClassName: UserServiceImpl
 * @Description：
 * @Author: wanghr
 * Date: 11:11 AM 2020/6/16
 * Version: 1.0
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {
    {
        HashMap<String, Object> userList = new HashMap<>();
        //记录 账号密码
        userList.put("王浩然","12345");
    }
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result<List<User>> selectAllUser() {
        Result<List<User>> result = new Result<>();
        List<User> users = userMapper.selectAllUser();
        return result;
    }

    @Override
    public Result<String> userEnroll(String username,String password) {
        Result<String> result = new Result<>();
        CheckParamEnums.PARAM_IS_NULL.assertNotNull(username);
        CheckParamEnums.PARAM_IS_NULL.assertNotNull(password);

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(password);
        log.info("加密前密码：{}", password);
        log.info("加密后密码：{}", encode);

        User user = new User();
        user.setUsername(username);
        user.setPassword(encode);
        log.info("user:{}",user);
        userMapper.insertSelective(user);

        result.setCode(ResultFlag.SUCESS.getCode());
        result.setMsg(ResultFlag.SUCESS.getMsg());
        return result;
    }
}
